package org.openotdr.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.HTML;

public class MainUI extends Composite implements HasText {

    private static MainUIUiBinder uiBinder = GWT.create(MainUIUiBinder.class);

    interface MainUIUiBinder extends UiBinder<Widget, MainUI> {
    }

    public MainUI() {
        initWidget(uiBinder.createAndBindUi(this));
    }
    @UiField FormPanel formPanel;
    @UiField FileUpload fileUpload;
    @UiField HTMLPanel panel;
    @UiField TextArea textArea;

    public MainUI(String firstName) {
        initWidget(uiBinder.createAndBindUi(this));
    }

    public void setText(String text) {

    }

    public String getText() {
        return "";
    }

    @UiHandler("button")
    void onButtonClick(ClickEvent event) {
        formPanel.addSubmitCompleteHandler(new SubmitCompleteHandler() {

            public void onSubmitComplete(SubmitCompleteEvent event) {
                //Window.alert("Complete!");
                String s = event.getResults();
                s = s.replace("<BR>", "\n");
                textArea.setText(s);
                //htmlPanel.setHTML(event.getResults());
            }
            
        });
        //formPanel.setAction("sr4731");
        formPanel.setAction("fileServlet");
        formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
        formPanel.setMethod(FormPanel.METHOD_POST);
        formPanel.submit();
    }
}
